package top.wyloop.springbatchexercise;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.job.builder.JobBuilder;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.step.builder.StepBuilder;
import org.springframework.batch.item.database.JdbcBatchItemWriter;
import org.springframework.batch.item.database.builder.JdbcBatchItemWriterBuilder;
import org.springframework.batch.item.file.FlatFileItemReader;
import org.springframework.batch.item.file.builder.FlatFileItemReaderBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;

/**
 * @author Wyatt6
 * @date 2025/6/19
 */
@Configuration
public class BatchConfiguration {
    /**
     * FlatFileItemReader<T>是一个框架内置的ItemReader接口的实现
     * 用于按行读取平面文件，T表示读取过程输出的数据类型
     */
    @Bean
    public FlatFileItemReader<Person> reader() {
        return new FlatFileItemReaderBuilder<Person>()
                .name("personItemReader")                               // reader的名称
                .resource(new ClassPathResource("sample-data.csv"))     // 输入数据源
                .delimited()                                            // 声明每行由分隔符(,)分开
                .names("firstName", "lastName")                         // 第一个数据对应firstName，第二个数据对应lastName
                .targetType(Person.class)                               // 输出的目标数据类型
                .build();                                               // 构造FlatFileItemReader实例
    }

    /**
     * 这是自定义的处理程序
     */
    @Bean
    public PersonItemProcessor processor() {
        return new PersonItemProcessor();
    }

    /**
     * JdbcBatchItemWriter<T>是一个框架内置的ItemWriter接口的实现
     * 用于将数据写入到JDBC数据库中，T表示准备要写入的数据类型
     *
     * @param dataSource 要写入的数据源
     */
    @Bean
    public JdbcBatchItemWriter<Person> writer(DataSource dataSource) {
        return new JdbcBatchItemWriterBuilder<Person>()
                .sql("INSERT INTO people (first_name, last_name) VALUES (:firstName, :lastName)")   // 定义写入的SQL语句
                .dataSource(dataSource)                                                             // 绑定数据源
                .beanMapped()                                                                       // beanMapped与columnMapped的区别？
                .build();
    }

    @Bean
    public Job importUserJob(JobRepository jobRepository, Step step1, JobCompletionNotificationListener listener) {
        return new JobBuilder("importUserJob", jobRepository)
                .listener(listener)
                .start(step1)
                .build();
    }

    @Bean
    public Step step1(JobRepository jobRepository, PlatformTransactionManager transactionManager,
                      FlatFileItemReader<Person> reader, PersonItemProcessor processor, JdbcBatchItemWriter<Person> writer) {
        return new StepBuilder("step1", jobRepository)
                .<Person, Person>chunk(3, transactionManager)
                .reader(reader)
                .processor(processor)
                .writer(writer)
                .build();
    }
}
